package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.fastjson.JSONObject;
import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.SysResultByPage;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.DepartmentDailyReport;
import com.blueocean_health.care.management.domain.dto.OrderRefundReqDto;
import com.blueocean_health.care.management.domain.dto.PayInfoListDto;
import com.blueocean_health.care.management.domain.dto.PayInfoReportDto;
import com.blueocean_health.care.management.domain.dto.PayInfoReqDto;
import com.blueocean_health.care.management.domain.ordervo.DepartmentReportVo;
import com.blueocean_health.care.management.domain.ordervo.OrderRefundVo;
import com.blueocean_health.care.management.domain.worktablevo.NurseWokerStatistics;
import com.blueocean_health.care.management.em.LashStatusEnum;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.service.IOrderPayService;
import com.github.pagehelper.Page;

/**
 * 对账单管理
 * 
 * @todo
 * @author 张亚林
 * @date 2018年8月20日 下午4:41:17
 */
@RestController
@RequestMapping("/web/order")
public class OrderPayController {

	@Autowired
	private IOrderPayService orderPayService;

	/**
	 * 对账单列表
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping("pay/list")
	public SysResult list(@RequestBody PayInfoReqDto dto) {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		dto.setHospitalId(user.getHospitalId());
		Page<PayInfoListDto> pageList = orderPayService.listRecord(dto);
		return SysResultByPage.ok(pageList.getResult(), pageList.getTotal());

	}

	/**
	 * 对账单详情
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping("pay/detail")
	public SysResult detail(@RequestBody PayInfoReqDto dto) {
		return SysResult.ok();

	}

	/**
	 * 扎帐
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping("pay/lash")
	public SysResult lash(@RequestBody PayInfoReqDto dto) {
		dto.setIsLash(LashStatusEnum._1.getKey());// 已扎帐
		Set<String> set = new HashSet<>();
		set.add(dto.getId());
		dto.setIds(set);
		orderPayService.updateLashStatus(dto);
		return SysResult.ok();

	}

	/**
	 * 批量扎帐
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping("pay/batchlash")
	public SysResult batchlash(@RequestBody PayInfoReqDto dto) {
		dto.setIsLash(LashStatusEnum._1.getKey());// 已扎帐
		orderPayService.updateLashStatus(dto);
		return SysResult.ok();

	}

	/**
	 * 取消扎帐
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping("pay/cancellash")
	public SysResult cancellash(@RequestBody PayInfoReqDto dto) {

		Set<String> set = new HashSet<>();
		set.add(dto.getId());
		dto.setIds(set);
		dto.setIsLash(LashStatusEnum._0.getKey());// 未扎帐
		orderPayService.updateLashStatus(dto);
		return SysResult.ok();

	}

	/**
	 * 对账表导出
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("pay/excel")
	public void getPayOrderExcel(PayInfoReqDto dto, HttpServletResponse response) throws Exception {
		try {
			User user = LocalManager.getVal(LocalManagerType.USER_WEB);
			dto.setHospitalId(user.getHospitalId());
			List<PayInfoReportDto> list = orderPayService.exportRecord(dto);
			if(list==null||list.size()==0){
				throw new RuntimeException("没有可导出的数据");
			}
			String finalFileName = URLEncoder.encode("对账单明细表_" + TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS) + ".xls",
					"UTF8");
			response.setContentType("application/force-download");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

			ExcelUtil.getInstance().exportObj2Excel(toClient, list, PayInfoReportDto.class);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 退款确认
	 * 
	 * @param orderPayVo
	 * @return
	 */
	@RequestMapping("refund/confirm")
	public SysResult refund(@RequestBody OrderRefundReqDto dto) {
		if (StringUtils.isEmpty(dto.getAmount()) || !StringUtils.isNumber(dto.getAmount())) {
			return SysResult.error("金额格式不正确，请检查后重试！");
		} else if ("0".equals(dto.getAmount()) || "0.00".equals(dto.getAmount())) {
			return SysResult.error("退款金额不能为0！");
		} else if (StringUtils.isEmpty(dto.getOrderId())) {
			return SysResult.error("订单号不能为空！");
		}
		orderPayService.refundDo(dto);
		return SysResult.ok();

	}

}
